package com.ai.mall.service.impl;

import com.ai.mall.dao.CartDao;
import com.ai.mall.entity.CartEntity;
import com.ai.mall.entity.UsersEntity;
import com.ai.mall.service.CartService;
import com.ai.mall.utils.PageUtils;
import com.ai.mall.utils.Query;
import com.ai.mall.utils.R;
import com.ai.mall.vo.req.AddCartProductInfoRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service("cartService")
public class CartServiceImpl extends ServiceImpl<CartDao, CartEntity> implements CartService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CartEntity> page = this.page(
                new Query<CartEntity>().getPage(params),
                new QueryWrapper<CartEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public String addToCart(AddCartProductInfoRequest productInfo, UsersEntity userInfo) {
        // 检查参数是否为空
        if (productInfo == null || productInfo.getProductId() == null || productInfo.getQuantity() == null) {
            return "参数错误";
        }

        // 创建购物车项实体
        CartEntity cartItem = new CartEntity();
        cartItem.setUserId(userInfo.getUserId());
        cartItem.setProductId(productInfo.getProductId());
        cartItem.setQuantity(productInfo.getQuantity());
        cartItem.setProductName(productInfo.getProductName());
        cartItem.setProductPrice(productInfo.getProductPrice());

        // 尝试从数据库中获取已存在的购物车项
        QueryWrapper<CartEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userInfo.getUserId());
        queryWrapper.eq("product_id", productInfo.getProductId());
        CartEntity existingCartItem = getBaseMapper().selectOne(queryWrapper);

        if (existingCartItem != null) {
            // 如果商品已经在购物车中，更新数量
            existingCartItem.setQuantity(existingCartItem.getQuantity() + productInfo.getQuantity());
            getBaseMapper().updateById(existingCartItem);
            return "success";
        } else {
            // 如果商品不在购物车中，插入新项
            getBaseMapper().insert(cartItem);
            return "success";
        }
    }

    @Override
    public R removeFromCart(Integer productId, UsersEntity userInfo) {
        // 根据 productId 查询购物车中的商品项
        QueryWrapper<CartEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userInfo.getUserId());
        queryWrapper.eq("product_id", productId);
        CartEntity existingCartItem = getBaseMapper().selectOne(queryWrapper);

        if (existingCartItem != null) {
            // 如果找到了商品项，则进行删除操作
            getBaseMapper().deleteById(existingCartItem.getCartId());
            return R.ok("商品已成功从购物车中移除");
        } else {
            // 如果商品项不存在于购物车中，返回错误信息
            return R.error("商品不在购物车中");
        }
    }

    @Override
    public R updateProductQuantity(Integer productId, Integer quantity, UsersEntity userInfo) {
        // 根据用户ID和商品ID查找购物车中的商品项
        QueryWrapper<CartEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userInfo.getUserId());
        queryWrapper.eq("product_id", productId);
        CartEntity existingCartItem = getBaseMapper().selectOne(queryWrapper);

        if (existingCartItem != null) {
            // 如果找到了商品项，则更新其数量
            existingCartItem.setQuantity(quantity);
            getBaseMapper().updateById(existingCartItem);
            return R.ok("商品数量已成功更新");
        } else {
            // 如果商品项不存在于购物车中，返回错误信息
            return R.error("商品不在购物车中");
        }
    }

    @Override
    public List<CartEntity> getCartProducts(UsersEntity userInfo) {
        // 根据用户ID查询购物车商品列表
        QueryWrapper<CartEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userInfo.getUserId());
        return getBaseMapper().selectList(queryWrapper);
    }

}